﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace FTUninstaller
{
    public partial class QTPUninstaller
    {
        RegistryItemClean KeyCleanerProgID = (List<string> keys, QTPUninstaller scan) =>
        {
            RegistryKey keyProgID = Registry.ClassesRoot;

            UpdateProgressEventArgs e = new UpdateProgressEventArgs(0, keys.Count, "Cleaning ProgID");
            int value = 0;

            if (keyProgID == null) return false;
            foreach (string key in keys)
            {
                keyProgID.DeleteSubKeyTree(key, true);
                e.Value = ++value;
                scan.OnUpdateProgress(e);
            }
            return false;
        };

        RegistryItemClean KeyCleanerCLSID = (List<string> keys, QTPUninstaller scan) =>
        {
            RegistryKey keyCLSID = Registry.ClassesRoot.OpenSubKey(scan.PathCLSID, true);
            if (keyCLSID == null) return false;

            UpdateProgressEventArgs e = new UpdateProgressEventArgs(0, keys.Count, "Cleaning CLSID");
            int value = 0;

            foreach (string key in keys)
            {
                keyCLSID.DeleteSubKeyTree(key, false);
                e.Value = ++value;
                scan.OnUpdateProgress(e);
            }
            return false;
        };

        RegistryItemClean KeyCleanerIID = (List<string> keys, QTPUninstaller scan) =>
        {
            RegistryKey keyIID = Registry.ClassesRoot.OpenSubKey(scan.PathIID, true);
            if (keyIID == null) return false;

            UpdateProgressEventArgs e = new UpdateProgressEventArgs(0, keys.Count, "Cleaning Interface ID");
            int value = 0;

            foreach (string key in keys)
            {
                keyIID.DeleteSubKeyTree(key, false);
                e.Value = ++value;
                scan.OnUpdateProgress(e);
            }
            return false;
        };

        RegistryItemClean KeyCleanerTypeLib = (List<string> keys, QTPUninstaller scan) =>
        {
            RegistryKey keyTypeLib = Registry.ClassesRoot.OpenSubKey(scan.PathTypeLib, true);
            if (keyTypeLib == null) return false;

            UpdateProgressEventArgs e = new UpdateProgressEventArgs(0, keys.Count, "Cleaning TypeLib");
            int value = 0;

            foreach (string key in keys)
            {
                keyTypeLib.DeleteSubKeyTree(key, false);
                e.Value = ++value;
                scan.OnUpdateProgress(e);
            }
            return false;
        };

        RegistryItemClean KeyCleanerAppID = (List<string> keys, QTPUninstaller scan) =>
        {
            RegistryKey keyAppID = Registry.CurrentUser.OpenSubKey(scan.PathAppID, true);
            if (keyAppID == null) return false;

            UpdateProgressEventArgs e = new UpdateProgressEventArgs(0, keys.Count, "Cleaning AppID");
            int value = 0;

            foreach (string key in keys)
            {
                keyAppID.DeleteSubKeyTree(key, false);
                e.Value = ++value;
                scan.OnUpdateProgress(e);
            }

            return false;
        };

        RegistryItemClean KeyCleanerInstalledComponents = (List<string> keys, QTPUninstaller scan) =>
        {
            int value = 0;

            foreach (string key in keys)
            {
                RegistryKey uninstallItem = Registry.LocalMachine.OpenSubKey(key + "\\InstallProperties");
                string sDisplayName = (string)uninstallItem.GetValue("DisplayName");
                String uninstallArgs = uninstallItem.GetValue("UninstallString").ToString().ToLower();
                if (uninstallArgs != null && uninstallArgs.Contains("msiexec"))
                {
                    // do a silent uninstall with no restart forced
                    uninstallArgs = uninstallArgs.Replace("/i", "").Replace("/x", "").Replace("/u", "");
                    uninstallArgs = uninstallArgs.Replace("msiexec.exe", "");
                    Process uninstallProc;
                    UpdateProgressEventArgs e = new UpdateProgressEventArgs(++value, keys.Count, "Uninstalling Component - " + sDisplayName);
                    uninstallProc = Process.Start("MsiExec.exe", "/norestart /passive /uninstall " + uninstallArgs);
                    scan.OnUpdateProgress(e);
                    uninstallProc.WaitForExit();
                }

            }
            return false;
        };


        public void PerformClean(List<string> keys, RegistryItemClean cleaner)
        {
            cleaner(keys, this);
        }

    }
}